<template>
  <div class="content_page">
    <div class="content_page_logo">
      <image src="/static/myInfo/logo@2x.png"></image>
    </div>
    <div class="content_page_btn">
      <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="one">
        一键授权绑定
      </button>
      <!-- <div class="two" @click="tobindPhone">手动绑定</div> -->
    </div>
  </div>
</template>
<script lang="ts" setup>
import { authPhone } from "@/api/myInfo/myInfo.ts";
import type { resInfo } from "@/api/myInfo/type.ts";
import { personInfoFn } from "@/api/myInfo/myInfo.ts";
import { onLoad } from "@dcloudio/uni-app";
import { ref,getCurrentInstance } from "vue";
const tobindPhone = () => {
  uni.navigateTo({
    url: "/pages/settingInfo/bindphone/codebind",
  });
};
const instance = getCurrentInstance();
const appStorage = instance?.appContext.config.globalProperties.appStorage;
const getData = () => {
		personInfoFn()
			.then((res : resInfo) => {
				if (res.resultStatus) {
          if (res.resultData) {
          if (fromPage.value == "pages/addDevice/addDevice") {
            uni.navigateBack();
          } else {
            uni.navigateTo({
              url: "/pages/settingInfo/index",
            });
          }
        }
				} else {
					uni.showToast({
						title: res.errorMessage,
						icon: "none",
					});
				}
			})
			.catch((err : any) => {
				uni.showToast({
					title: "服务器异常",
					icon: "none",
				});
			});
};
const getPhoneNumber = (e: any) => {
  authPhone({
    code: e.detail.code,
    encryptedData: e.detail.encryptedData,
    iv: e.detail.iv,
  })
    .then((res: resInfo) => {
      if (res.resultStatus) {
        getData()
      } else {
        uni.showToast({
          title: res.errorMessage,
          icon: "none",
        });
      }
    })
    .catch((err: any) => {
      console.error(err);
      uni.showToast({
        title: "服务器异常",
        icon: "none",
      });
    });
};
const fromPage = ref("");
onLoad((options) => {
  const pages = getCurrentPages();
  const prevPage = pages[pages.length - 2];
  fromPage.value = prevPage.route;

});
</script>
<style lang="scss" scoped>
.content_page {
  background: #f5f6f7;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  &_logo {
    image {
      width: 360rpx;
      height: 100rpx;
    }
  }

  &_tip {
    font-size: 40rpx;
    text-align: center;
    color: #1f1f1f;
    line-height: 56rpx;
    margin: 30rpx auto 0;
  }

  &_btn {
    margin: 110rpx auto;

    .one {
      width: 500rpx;
      height: 88rpx;
      background: linear-gradient(90deg, #00a67e, #006d56);
      line-height: 88rpx;
      text-align: center;
      border-radius: 44rpx;
      margin-bottom: 30rpx;
      color: #fff;
    }

    .two {
      width: 500rpx;
      height: 88rpx;
      background-color: #fff;
      line-height: 88rpx;
      text-align: center;
      border-radius: 44rpx;
    }
  }
}
</style>
